package edu.hawaii.duedates.tests;

import static org.junit.Assert.assertEquals;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import org.junit.Before;
import org.junit.Test;

import edu.hawaii.duedates.Book;
import edu.hawaii.duedates.Library;
import edu.hawaii.duedates.LibraryManager;
import edu.hawaii.duedates.libraries.HSLLibrary;
import edu.hawaii.duedates.libraries.UHMLibrary;

/**
 * TestLibraryManager is responsible for testing methods and fields of the LibraryManager class.
 * @author Anthony M. Du
 *
 */
public class TestLibraryManager {
  private LibraryManager libMgmt;
  private final String[] args = {"-v", "-uhm", "name", "password",
                                 "-hsl", "name2", "password2", "-sort", "duedate", "-within", "5"};
  
  /**
   * SetUp variables for testing LibraryManager class.
   * @throws Exception
   */
  @Before
  public void setUp() {
    libMgmt = new LibraryManager(args);
  }

  /**
   * Test getLibraries method.
   */
  @Test
  public void testGetLibraries() {
    List<Library> libs = libMgmt.getLibraries();
    assertEquals("Testing set of libraries count should be 2", 2, libs.size());
    assertEquals("Testing library \"-hsl\" exists", "Hawaii State Library", 
        libs.get(0).getLibName());
    assertEquals("Testing library \"-uhm\" exists", "University of Hawaii at Manoa Library",
        libs.get(1).getLibName());
  }
  
  /**
   * Test library book sort options. 
   * This test will test the ability to sort by due dates and arrange by the within option. 
   * This test is done via method call of getBooks() of LibraryManager.
   */
  @Test
  public void testLibrarySortOptions() {
    String dateFormat = "MM-dd-yy";
    SimpleDateFormat formattedDate = new SimpleDateFormat(dateFormat, Locale.US);
    Library uhmLib = new UHMLibrary();
    Library hslLib = new HSLLibrary();
    
    try {
      Book book1 = new Book("Railspace", formattedDate.parse("11-12-08"));
      Book book2 = new Book("Professional LAMP", formattedDate.parse("11-02-08"));
      Book book3 = new Book("Programming Languages", formattedDate.parse("11-01-08"));
      uhmLib.addBook(book1);
      uhmLib.addBook(book2);
      hslLib.addBook(book3);
      // Testing duedate option
      assertEquals("Testing duedate order of first book", book2, libMgmt.getBooks(uhmLib).get(0));
      assertEquals("Testing duedate order of second book", book1, libMgmt.getBooks(uhmLib).get(1));
      assertEquals("Testing duedate order of one book", book3, libMgmt.getBooks(hslLib).get(0));
      //Testing within option
      //This portion of the test is to see if the within option is valid for a certain range in time
      //Since time is continuous, this is commented out for developers to specify their own values.
//      Book book4 = new Book("Java 2 v5.0 Reference Guide", formattedDate.parse("11-23-08"));
//      uhmLib.addBook(book4);
//      assertEquals("Testing within option should return a book count of only 2", 
//                      2, libMgmt.getBooks(uhmLib).size());
    } 
    catch (ParseException e) {
      e.printStackTrace();
    }
  }
}
